<?php

/*
 * This file is part of the symfony package.
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
 * (c) 2004-2006 Sean Kerr <sean@code-box.org>
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * sfDateValidator verifies a parameter is of a date format.
 *
 * WARNING: This class is deprecated and will be removed in symfony 1.2.
 *
 * @package    symfony
 * @subpackage validator
 * @author     Nick Lane <nick.lane@internode.on.net>
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @author     Sean Kerr <sean@code-box.org>
 * @version    SVN: $Id: sfDateValidator.class.php 10270 2008-07-13 21:51:03Z Carl.Vondrick $
 * @deprecated Deprecated since symfony 1.1
 */
class myArrayNumberValidator extends sfValidator
{
  /**
   * Execute this validator.
   *
   * @param mixed A file or parameter value/array
   * @param error An error message reference
   *
   * @return bool true, if this validator executes successfully, otherwise false
   */
  public function execute(&$value, &$error)
  {
    if (!isset($value) || !$value || !count($value)) {    
      return true;
    }
    for ($i = 0; $i < count($value); $i++){
        if (!isset($value[$i]) || $value[$i] == null) {
            $error = $this->getParameter('required_msg');
            $error= str_replace('%i', $i +1, $error);
            return false;
        }
        if (!preg_match('/^-?\d+(\.\d+)?$/', $value[$i]) && $value[$i] !=''  && $value[$i] != null )           {
          // it's NaN, what nerve!
          $error = $this->getParameterHolder()->get('nan_error');
          $error= str_replace('%i', $i +1, $error);
    
          return false;
        }
        if ($value[$i] <=0){
            $error =$this->getParameterHolder()->get('numberGt0_msg');
            $error= str_replace('%i', $i +1, $error);
            return  false;
        }
        
        $compareValue = $this->getContext()->getRequest()->getParameter($this->getParameterHolder()->get('ltColumn'));
        if ($compareValue && $compareValue[$i] >= 0 && count($compareValue)){
            if ($value[$i] > $compareValue[$i]){
                $error = $this->getParameterHolder()->get('ltColumn_msg');
                $error= str_replace('%i', $i +1, $error);
                return false;
            }
        }
    }
    return true;
  }

  /**
   * Initializes the validator.
   *
   * @param sfContext The current application context
   * @param array   An associative array of initialization parameters
   *
   * @return bool true, if initialization completes successfully, otherwise false
   */
  public function initialize($context, $parameters = null)
  {
    // Initialize parent
    parent::initialize($context, $parameters);

    // Set defaults
    $this->getParameterHolder()->set('required_msg', 'Component %i is empty');
    $this->getParameterHolder()->set('numberGt0_msg', 'The qty of component %i must be greater than 0');
    $this->getParameterHolder()->set('nan_error', 'The qty of component %i must be a number');

    $this->getParameterHolder()->add($parameters);

    return true;
  }
}


class myArrayNullFormatNumberValidator extends sfValidator
{
  /**
   * Execute this validator.
   *
   * @param mixed A file or parameter value/array
   * @param error An error message reference
   *
   * @return bool true, if this validator executes successfully, otherwise false
   */
  public function execute(&$value, &$error)
  {
    if (!isset($value) || !$value || !count($value)) {
      return true;
    }
    $atleastGt0 = $this->getParameterHolder()->get('atLeastGt0');
    $b_atleastGt0 = $atleastGt0 ?  false : true;

    for ($i = 0; $i < count($value); $i++){
        $value[$i] = CommonValues::CurrencyToNumber($value[$i]);

        if (!preg_match('/^-?\d+(\.\d+)?$/', $value[$i]) && $value[$i] !=''  && $value[$i] != null )           {
          // it's NaN, what nerve!
          $error = $this->getParameterHolder()->get('nan_error');
          $error= str_replace('%i', $i +1, $error);

          return false;
        }
        if ($value[$i] <0){
            $error =$this->getParameterHolder()->get('numberGt0_msg');
            $error= str_replace('%i', $i +1, $error);
            return  false;
        }
        if ($value[$i] > 0) {
            $b_atleastGt0 = true;
        }
        $compareValue = $this->getContext()->getRequest()->getParameter($this->getParameterHolder()->get('ltColumn'));
        if ($compareValue && $compareValue[$i] >= 0 && count($compareValue)){
            if ($value[$i] > $compareValue[$i]){
                $error = $this->getParameterHolder()->get('ltColumn_msg');
                $error= str_replace('%i', $i +1, $error);
                return false;
            }
        }
    }

    if (!$b_atleastGt0 ){
            $error = $this->getParameterHolder()->get('atLeastGt0_msg');
            return false;
    }
    return true;
  }

  /**
   * Initializes the validator.
   *
   * @param sfContext The current application context
   * @param array   An associative array of initialization parameters
   *
   * @return bool true, if initialization completes successfully, otherwise false
   */
  public function initialize($context, $parameters = null)
  {
    // Initialize parent
    parent::initialize($context, $parameters);

    // Set defaults
    $this->getParameterHolder()->set('numberGt0_msg', 'The qty of component %i must be greater than 0');
    $this->getParameterHolder()->set('nan_error', 'The qty of component %i must be a number');

    $this->getParameterHolder()->add($parameters);

    return true;
  }
}


class myArrayNullNumberValidator extends sfValidator
{
  /**
   * Execute this validator.
   *
   * @param mixed A file or parameter value/array
   * @param error An error message reference
   *
   * @return bool true, if this validator executes successfully, otherwise false
   */
  public function execute(&$value, &$error)
  {
    if (!isset($value) || !$value || !count($value)) {    
      return true;
    }
    $atleastGt0 = $this->getParameterHolder()->get('atLeastGt0');
    $b_atleastGt0 = $atleastGt0 ?  false : true;
    
    for ($i = 0; $i < count($value); $i++){
    
        if (!preg_match('/^-?\d+(\.\d+)?$/', $value[$i]) && $value[$i] !=''  && $value[$i] != null )           {
          // it's NaN, what nerve!
          $error = $this->getParameterHolder()->get('nan_error');
          $error= str_replace('%i', $i +1, $error);
    
          return false;
        }
        if ($value[$i] <0){
            $error =$this->getParameterHolder()->get('numberGt0_msg');
            $error= str_replace('%i', $i +1, $error);
            return  false;
        }
        if ($value[$i] > 0) {
            $b_atleastGt0 = true;
        }
        $compareValue = $this->getContext()->getRequest()->getParameter($this->getParameterHolder()->get('ltColumn'));
        if ($compareValue && $compareValue[$i] >= 0 && count($compareValue)){
            if ($value[$i] > $compareValue[$i]){
                $error = $this->getParameterHolder()->get('ltColumn_msg');
                $error= str_replace('%i', $i +1, $error);
                return false;
            }
        }
        $compareValue1 = $this->getContext()->getRequest()->getParameter($this->getParameterHolder()->get('ltColumn1'));
        $compareValue2 = $this->getContext()->getRequest()->getParameter($this->getParameterHolder()->get('ltColumn2'));
        if ($compareValue1 && $compareValue2 && count($compareValue1) && count($compareValue2)){
            if ($value[$i] > $compareValue1[$i] - $compareValue2[$i]){
                $error = $this->getParameterHolder()->get('ltColumn_msg');
                $error= str_replace('%i', $i +1, $error);
                return false;
            }
        }
    }    
    
    if (!$b_atleastGt0 ){
            $error = $this->getParameterHolder()->get('atLeastGt0_msg');
            return false;
    }
    return true;
  }

  /**
   * Initializes the validator.
   *
   * @param sfContext The current application context
   * @param array   An associative array of initialization parameters
   *
   * @return bool true, if initialization completes successfully, otherwise false
   */
  public function initialize($context, $parameters = null)
  {
    // Initialize parent
    parent::initialize($context, $parameters);

    // Set defaults    
    $this->getParameterHolder()->set('numberGt0_msg', 'The qty of component %i must be greater than 0');
    $this->getParameterHolder()->set('nan_error', 'The qty of component %i must be a number');

    $this->getParameterHolder()->add($parameters);

    return true;
  }
}

class myArrayNullDateValidator extends sfValidator
{
  /**
   * Execute this validator.
   *
   * @param mixed A file or parameter value/array
   * @param error An error message reference
   *
   * @return bool true, if this validator executes successfully, otherwise false
   */
  public function execute(&$value, &$error)
  {
    if (!isset($value) || !$value || !count($value)) {
      return true;
    }

    $culture = $this->context->getUser()->getCulture();
    
    for ($i = 0; $i < count($value); $i++){
        if (!$value[$i]) continue;
        // Validate the given date
        $value1 = $this->getValidDate($value[$i], $culture);
        if (!$value1)
        {
          $error = $this->getParameter('date_error');

          $error= str_replace('%i', $i +1, $error);
          return false;
        }
    }

    return true;
  }

  protected function getValidDate($value, $culture)
  {
    // Use the language culture date format
    $result = $this->context->getI18N()->getDateForCulture($value, $culture);
    list($y, $m, $d) = $result;

    // Make sure the date is a valid gregorian calendar date also
    if ($result === null || !checkdate($m, $d, $y))
    {
      return null;
    }

    return strtotime("$y-$m-$d 00:00");
  }

  /**
   * Initializes the validator.
   *
   * @param sfContext The current application context
   * @param array   An associative array of initialization parameters
   *
   * @return bool true, if initialization completes successfully, otherwise false
   */
  public function initialize($context, $parameters = null)
  {
    // Initialize parent
    parent::initialize($context, $parameters);

    // Set defaults
    $this->getParameterHolder()->set('date_error', 'You have entered an invalid date, row %i');
    $this->getParameterHolder()->add($parameters);

    return true;
  }
}



class sfNumberFormatValidator extends sfValidator
{
  /**
   * Executes this validator.
   *
   * @param mixed A file or parameter value/array
   * @param error An error message reference
   *
   * @return bool true, if this validator executes successfully, otherwise false
   */
  public function execute(&$value, &$error)
  {
      $value = CommonValues::CurrencyToNumber($value);
    if (!preg_match('/^-?\d+(\.\d+)?$/', $value))
    {
      // it's NaN, what nerve!
      $error = $this->getParameterHolder()->get('nan_error');

      return false;
    }

    $type = strtolower($this->getParameterHolder()->get('type'));

    switch ($type)
    {
      case "decimal":
      case "float":
      {
        if (substr_count($value, '.') != 1)
        {
          // value isn't a float, shazbot!
          $error = $this->getParameterHolder()->get('type_error');
          return false;
        }

        // cast our value to a float
        $value = (float) $value;

        break;
      }

      case "int":
      case "integer":
      {
        // Note: (Both 3 AND 3.0 are BOTH considered integers and 3.1 is not)
        if ((float) $value != (int) $value)
        {
          // is not an integer.
          $error = $this->getParameterHolder()->get('type_error');
          return false;
        }

        // cast our value to an integer
        $value = (int) $value;

        break;
      }

    }

    $min = $this->getParameterHolder()->get('min');

    if ($min !== null && $value < $min)
    {
      // too small
      $error = $this->getParameterHolder()->get('min_error');

      return false;
    }

    $max = $this->getParameterHolder()->get('max');

    if ($max !== null && $value > $max)
    {
      // too large
      $error = $this->getParameterHolder()->get('max_error');

      return false;
    }

    return true;
  }

  /**
   * Initializes this validator.
   *
   * @param sfContext The current application context
   * @param array   An associative array of initialization parameters
   *
   * @return bool true, if initialization completes successfully, otherwise false
   */
  public function initialize($context, $parameters = null)
  {
    // initialize parent
    parent::initialize($context);

    // set defaults
    $this->getParameterHolder()->set('max',        null);
    $this->getParameterHolder()->set('max_error',  'Input is too large');
    $this->getParameterHolder()->set('min',        null);
    $this->getParameterHolder()->set('min_error',  'Input is too small');
    $this->getParameterHolder()->set('nan_error',  'Input is not a number');
    $this->getParameterHolder()->set('type',       'any');
    $this->getParameterHolder()->set('type_error', 'Input is not a number');

    $this->getParameterHolder()->add($parameters);

    // check user-specified parameters
    $type = strtolower($this->getParameterHolder()->get('type'));

    // array of allowed types
    $allowed_types = array('any', 'decimal', 'float', 'int', 'integer');

    if (!in_array(strtolower($type), $allowed_types))
    {
      // unknown type
      throw new sfValidatorException(sprintf('Unknown number type "%s" in NumberValidator.', $this->getParameterHolder()->get('type')));
    }

    return true;
  }
}